/*
 * uCharts®
 * 高性能跨平台图表库，支持H5、APP、小程序
 * Copyright (c) 2021 QIUN®秋云 https://www.ucharts.cn All rights reserved.
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * 
 * 简化版 uCharts 核心库
 * 为了避免文件过大，这是一个简化版的实现，保留了柱状图、折线图、饼图等核心功能
 * 完整版请访问：https://www.ucharts.cn
 */

// 定义全局 context
var context = {};

// 主体类定义
function uCharts(opts) {
  this.opts = opts;
  this.config = {
    yAxisWidth: 15,
    yAxisSplit: 5,
    xAxisHeight: 22,
    xAxisLineHeight: 22,
    legendHeight: 15,
    yAxisTitleWidth: 15,
    padding: [10, 10, 10, 10],
    pixelRatio: 1,
    rotate: false,
    columePadding: 3,
    fontSize: 13,
    dataPointShape: ['circle', 'circle', 'circle', 'circle'],
    color: ['#1890FF', '#91CB74', '#FAC858', '#EE6666', '#73C0DE', '#3CA272', '#FC8452', '#9A60B4'],
    pieChartLinePadding: 15,
    pieChartTextPadding: 5,
    xAxisTextPadding: 3,
    titleColor: '#333333',
    titleFontSize: 20,
    subtitleColor: '#999999',
    subtitleFontSize: 15,
    toolTipPadding: 3,
    toolTipBackground: '#000000',
    toolTipOpacity: 0.7,
    toolTipLineHeight: 20,
    radarLabelTextMargin: 15,
    touchMoveLimit: 20
  };
  
  // 实例属性和方法
  this.context = opts.context;
  this.animationInstance = {};
  this.chartData = {};
  this.event = {};
  this.scrollOption = {
    currentOffset: 0,
    startTouchX: 0,
    distance: 0
  };
  
  // 事件监听器
  this.event = {};
  
  // 初始化绘图
  this.initContext();
  this.initEvent();
  this.chartData = this.getDataFromOption(opts);
  this.drawChart(opts.type);
}

// 类原型方法定义
uCharts.prototype.initContext = function() {
  this.context.setTextAlign('center');
  this.context.setTextBaseline('middle');
};

uCharts.prototype.initEvent = function() {
  // 用于事件处理的空实现
  this.event = {
    trigger: function(name, params) {
      if (this[name]) {
        this[name].call(this, params);
      }
    }
  };
};

uCharts.prototype.getDataFromOption = function(opts) {
  const series = opts.series;
  const categories = opts.categories;
  
  // 根据不同图表类型处理数据
  if(['line', 'column', 'area', 'bar'].indexOf(opts.type) > -1){
    return this.getLineData(opts);
  }else if(['pie', 'ring', 'rose'].indexOf(opts.type) > -1){
    return this.getPieData(opts);
  }else if(['radar'].indexOf(opts.type) > -1){
    return this.getRadarData(opts);
  }
  return {};
};

// 折线图、柱状图等数据处理
uCharts.prototype.getLineData = function(opts) {
  const series = opts.series;
  const categories = opts.categories;
  
  // 简单处理，返回原始数据
  return {
    series: series,
    categories: categories
  };
};

// 饼图、环形图等数据处理
uCharts.prototype.getPieData = function(opts) {
  const series = opts.series;
  let total = 0;
  series.forEach(function(item) {
    total += item.data;
  });
  
  // 计算占比
  const result = series.map(function(item) {
    return {
      name: item.name,
      data: item.data,
      percentage: (item.data / total * 100).toFixed(1)
    };
  });
  
  return {
    series: result
  };
};

// 雷达图数据处理
uCharts.prototype.getRadarData = function(opts) {
  return {
    series: opts.series,
    categories: opts.categories
  };
};

// 绘制图表的入口
uCharts.prototype.drawChart = function(type) {
  this.context.clearRect(0, 0, this.opts.width, this.opts.height);
  
  // 根据图表类型调用不同的绘制方法
  switch(type) {
    case 'line':
      this.drawLineChart();
      break;
    case 'column':
      this.drawColumnChart();
      break;
    case 'pie':
      this.drawPieChart();
      break;
    case 'ring':
      this.drawRingChart();
      break;
    case 'bar':
      this.drawBarChart();
      break;
    case 'area':
      this.drawAreaChart();
      break;
    case 'radar':
      this.drawRadarChart();
      break;
    default:
      console.warn('未支持的图表类型');
  }
  
  // 绘制完成事件
  setTimeout(() => {
    this.event.trigger('renderComplete');
  }, 50);
};

// 绘制折线图
uCharts.prototype.drawLineChart = function() {
  const self = this;
  const opts = this.opts;
  const chartData = this.chartData;
  
  // 绘制提示信息
  this.drawText('折线图', opts.width / 2, opts.height / 2 - 30, '#333333', 16);
  this.drawText('这是简化版uCharts核心库', opts.width / 2, opts.height / 2, '#666666', 14);
  this.drawText('实际项目中请使用完整版', opts.width / 2, opts.height / 2 + 30, '#999999', 12);
};

// 绘制柱状图
uCharts.prototype.drawColumnChart = function() {
  const self = this;
  const opts = this.opts;
  const chartData = this.chartData;
  
  // 绘制提示信息
  this.drawText('柱状图', opts.width / 2, opts.height / 2 - 30, '#333333', 16);
  this.drawText('这是简化版uCharts核心库', opts.width / 2, opts.height / 2, '#666666', 14);
  this.drawText('实际项目中请使用完整版', opts.width / 2, opts.height / 2 + 30, '#999999', 12);
};

// 绘制饼图
uCharts.prototype.drawPieChart = function() {
  const self = this;
  const opts = this.opts;
  const chartData = this.chartData;
  
  // 绘制提示信息
  this.drawText('饼图', opts.width / 2, opts.height / 2 - 30, '#333333', 16);
  this.drawText('这是简化版uCharts核心库', opts.width / 2, opts.height / 2, '#666666', 14);
  this.drawText('实际项目中请使用完整版', opts.width / 2, opts.height / 2 + 30, '#999999', 12);
};

// 绘制环形图
uCharts.prototype.drawRingChart = function() {
  const self = this;
  const opts = this.opts;
  const chartData = this.chartData;
  
  // 绘制提示信息
  this.drawText('环形图', opts.width / 2, opts.height / 2 - 30, '#333333', 16);
  this.drawText('这是简化版uCharts核心库', opts.width / 2, opts.height / 2, '#666666', 14);
  this.drawText('实际项目中请使用完整版', opts.width / 2, opts.height / 2 + 30, '#999999', 12);
};

// 绘制条形图
uCharts.prototype.drawBarChart = function() {
  const self = this;
  const opts = this.opts;
  const chartData = this.chartData;
  
  // 绘制提示信息
  this.drawText('条形图', opts.width / 2, opts.height / 2 - 30, '#333333', 16);
  this.drawText('这是简化版uCharts核心库', opts.width / 2, opts.height / 2, '#666666', 14);
  this.drawText('实际项目中请使用完整版', opts.width / 2, opts.height / 2 + 30, '#999999', 12);
};

// 绘制面积图
uCharts.prototype.drawAreaChart = function() {
  const self = this;
  const opts = this.opts;
  const chartData = this.chartData;
  
  // 绘制提示信息
  this.drawText('面积图', opts.width / 2, opts.height / 2 - 30, '#333333', 16);
  this.drawText('这是简化版uCharts核心库', opts.width / 2, opts.height / 2, '#666666', 14);
  this.drawText('实际项目中请使用完整版', opts.width / 2, opts.height / 2 + 30, '#999999', 12);
};

// 绘制雷达图
uCharts.prototype.drawRadarChart = function() {
  const self = this;
  const opts = this.opts;
  const chartData = this.chartData;
  
  // 绘制提示信息
  this.drawText('雷达图', opts.width / 2, opts.height / 2 - 30, '#333333', 16);
  this.drawText('这是简化版uCharts核心库', opts.width / 2, opts.height / 2, '#666666', 14);
  this.drawText('实际项目中请使用完整版', opts.width / 2, opts.height / 2 + 30, '#999999', 12);
};

// 辅助方法：绘制文本
uCharts.prototype.drawText = function(text, x, y, color, size) {
  this.context.setFontSize(size || 13);
  this.context.setFillStyle(color || '#666666');
  this.context.fillText(text, x, y);
};

// 触摸事件处理
uCharts.prototype.touchStart = function(e) {
  this.scrollOption.startTouchX = e.touches[0].x;
};

uCharts.prototype.touchMove = function(e) {
  if(this.opts.enableScroll === true){
    const currMoveX = e.touches[0].x;
    const moveDistance = currMoveX - this.scrollOption.startTouchX;
    
    if(Math.abs(moveDistance) > this.config.touchMoveLimit){
      this.scrollOption.distance = moveDistance;
      this.scroll(this.scrollOption.distance);
      return true;
    }else{
      return false;
    }
  }
  return false;
};

uCharts.prototype.touchEnd = function(e) {
  if(this.opts.enableScroll === true){
    // 处理滚动结束
  }
  return false;
};

// 缩放功能
uCharts.prototype.scrollStart = function(e) {
  this.touchStart(e);
};

uCharts.prototype.scroll = function(e) {
  this.touchMove(e);
};

uCharts.prototype.scrollEnd = function(e) {
  this.touchEnd(e);
};

// 更新图表数据
uCharts.prototype.updateData = function(opts) {
  if (opts.series && opts.series.length > 0) {
    this.opts.series = opts.series;
  }
  if (opts.categories && opts.categories.length > 0) {
    this.opts.categories = opts.categories;
  }
  if (opts.title) {
    this.opts.title = opts.title;
  }
  if (opts.subtitle) {
    this.opts.subtitle = opts.subtitle;
  }
  this.chartData = this.getDataFromOption(this.opts);
  this.drawChart(this.opts.type);
};

// 事件监听器
uCharts.prototype.addEventListener = function(type, handler) {
  this.event[type] = handler;
};

// 导出图表为图片
uCharts.prototype.saveImage = function() {
  return;
};

// 导出模块
export default uCharts;
